package com.example.demo.utilServcie;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.codec.digest.DigestUtils;

public class SignUtils {

	public static String getSign(Map<String, String> params) {

		Set<String> set = params.keySet();

		List<String> list = new ArrayList<String>(set);

		Collections.sort(list);
		// 这种打印出的字符串顺序和微信官网提供的字典序顺序是一致的
		// 构造签名键值对的格式
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i) + "=" + params.get(list.get(i)));
			if (i == list.size() - 1) {
				sb.append(list.get(i) + "=" + params.get(list.get(i)));
			} else {
				sb.append(list.get(i) + "=" + params.get(list.get(i)) + "&");
			}
		}
		String SignTemp = sb + "&key=377729078cacd0711f00d4df8df260a3";
		// 进行MD5加密
		String result = DigestUtils.md5Hex(SignTemp).toUpperCase();
		return result;
	}
}